package com.ruoyi.mall.product.interfaces.facade;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.mall.product.domain.entity.PmsBrand;
import com.ruoyi.mall.product.domain.entity.PmsCategoryBrandRelation;
import com.ruoyi.mall.product.domain.vo.PmsBrandVo;
import com.ruoyi.mall.product.service.PmsBrandCategoryRelationAppService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: zqs-mall
 * @description:
 * @author: Mr.Zhang
 * @create: 2022-06-08 17:12
 **/
@RestController
@RequestMapping("/relation")
public class PmsCategoryBrandRelationController extends BaseController {
    @Autowired
    private PmsBrandCategoryRelationAppService pmsBrandCategoryRelationAppService;

    @GetMapping("brandCategoryRelation/list/{brandId}")
    public TableDataInfo list(@PathVariable("brandId") Long brandId) {
        startPage();
        List<PmsCategoryBrandRelation> pmsCategoryBrandRelations = pmsBrandCategoryRelationAppService.selectByBrandId(brandId);
        return getDataTable(pmsCategoryBrandRelations);
    }

    /**
     * 获取当前分类下的所有品牌
     *
     * @return
     */
    @GetMapping("brandCategoryRelation/brands/list")
    public AjaxResult listBrands(@RequestParam(value = "catId", required = true) Long catId) {
        List<PmsBrand> pmsCategoryBrandRelations = pmsBrandCategoryRelationAppService.selectByCatId(catId);

        List<PmsBrandVo> list = pmsCategoryBrandRelations.stream().map(i -> {
            PmsBrandVo pmsBrandVo = new PmsBrandVo();
            pmsBrandVo.setBrandId(i.getId());
            pmsBrandVo.setBrandName(i.getName());
            return pmsBrandVo;
        }).collect(Collectors.toList());

        return AjaxResult.success(list);
    }

    @PostMapping("brandCategoryRelation/save")
    public AjaxResult add(@RequestBody PmsCategoryBrandRelation pmsCategoryBrandRelation) {
        return toAjax(pmsBrandCategoryRelationAppService.insertPmsCategoryBrand(pmsCategoryBrandRelation));
    }

    @DeleteMapping("brandCategoryRelation/delete/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        return toAjax(pmsBrandCategoryRelationAppService.deleteById(id));
    }
}
